//
//  DuiJIZhuZView.m
//  WRY
//
//  Created by zhong sckj on 10-11-3.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DuiJIZhuZView.h"
#import "CommonJson.h"
#import "WebPath.h"
#import "AppDelegate_iPad.h"
#import "SCCharts.h"
#import "StaticVariable.h"

extern NSString *namecode;
extern NSString *nameStr;
@implementation DuiJIZhuZView
@synthesize listData,enterdetailCode,enterName;
@synthesize rootVc;


- (void)requestForRegion:(NSString *)body methodName:(NSString *)mthod {
	linkState=YES;
	[self showLoading];
	NSString *bodyStr = [NSString stringWithFormat:@"LoginGuid=%@&%@",[[AppDelegate_iPad currentDelegate] GUID],body];
	[[CommonJson sharedInstance] requestToPath:[WebPath enterprisePath]
									methodName:mthod 
									bodyString:bodyStr 
									 targetObj:self
							  targetMethodName:@"eventForRequest:"
									notifiName:@"Statistic__EnterpriseCanton"];
	
}
- (void)eventForRequest:(NSNotification *)notification {
	[self hideLoading];
	[[CommonJson sharedInstance] removeTarget:self notiName:[notification name]];
	NSObject *object  = [notification object];
	if ([object isKindOfClass:[NSDictionary class]]) {
		NSDictionary *dict = (NSDictionary *)object;
		if ([[dict objectForKey:@"Status"] intValue]==1)
		{
			if ([[dict objectForKey:@"Data"] isKindOfClass:[NSArray class]])
			{
				NSArray *data=(NSArray *)[dict objectForKey:@"Data"];
				[listData setArray:data];
				linkState=NO;
				[self drawHistogramView];
			}
        }
		
	}
}

-(void)linkData:(NSInteger)detailtypecode
{
	
	switch (detailtypecode)
	{
		case 1:
			enterdetailCode=@"01";
			enterName=@"废水污染源";
			break;
		case 2:
			enterdetailCode=@"02";
			enterName=@"废气污染源";
			break;
		case 3:
			enterdetailCode=@"03";
			enterName=@"污水处理厂";
			break;
		default:
			enterdetailCode=@"";
			enterName=@"污染源";
			break;
	}
	
	NSString *body = [NSString stringWithFormat:@"EnterpriseTypeCodes=%@&CantonLevel=%d",enterdetailCode,([[[AppDelegate_iPad currentDelegate] userType]intValue]+1)];
	NSString *methodName=@"Statistic__EnterpriseCanton";
	if (linkState==NO) {
		[self requestForRegion:body methodName:methodName];
	}

}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
		listData=[[NSMutableArray alloc]init];
	}
	
    return self;
}
#pragma mark -
#pragma mark 画堆积柱状图
-(void)drawHistogramView
{
	[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tuijiframe.png"]]];
	//画标题
	UIButton *titlebtn=[[UIButton alloc]initWithFrame:CGRectMake(20.0, 30.0, 300, 23)];
	NSString *titleStr=[NSString stringWithFormat:@"%@各地区%@状况",[[AppDelegate_iPad currentDelegate] showCantonName],enterName];
	[titlebtn setBackgroundImage:[UIImage imageNamed:@"coverflowmenu.png"] forState:UIControlStateNormal];
	[titlebtn setTitle:titleStr forState:UIControlStateNormal];
	[titlebtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[titlebtn setUserInteractionEnabled:NO];
	[self addSubview:titlebtn];
	[titlebtn release];
	
	NSMutableArray *dataarray=[[NSMutableArray alloc]init];

	for (NSDictionary * dir in self.listData) 
	{
		if ([[dir objectForKey:@"CantonCodes"]length]>15) {
			continue;
		}
		else 
		{
			NSString *cantonName=[dir objectForKey:@"CantonName"];
			NSString *chaobiao=[dir objectForKey:@"SuperscaleCount"];
			NSString *guzhang=[dir objectForKey:@"TroubleCount"];
			//NSString *total=[dir objectForKey:@"EnterpriseCount"];
			NSString *zhengchang=[dir objectForKey:@"NormalCount"];
			//NSString *zhengchang=[NSString stringWithFormat:@"%d",
//								  [total intValue]-[chaobiao intValue]-[guzhang intValue]];
			SC_StackBarChartData* statckbarChar=[[SC_StackBarChartData alloc]init];
			statckbarChar.title=cantonName;
			statckbarChar.over=[chaobiao intValue];
			statckbarChar.normal=[zhengchang intValue];
			statckbarChar.closed=[guzhang intValue];
			[dataarray addObject:statckbarChar];
			[statckbarChar release];
		}
	}
	SC_StackBarChartVC *stackVc=[[SC_StackBarChartVC alloc]init];
	[stackVc setViewFrame:CGRectMake(13.0f, 80.0f, 760.0f, 380.0f)];
	[self addSubview:stackVc.view];
	[stackVc setViewDelegate:self];
	[stackVc resetData:dataarray];
	
	[stackVc release];
	[dataarray release];
			
}
- (void)stackBarIsTouchedAtIndex:(NSInteger )index
{
	NSDictionary *dict=[listData objectAtIndex:index+1];
	NSString *selectNamecode;
	NSString *selectName;
	selectNamecode=[dict objectForKey:@"CantonCodes"];
	selectName=[dict objectForKey:@"CantonName"];
	namecode=selectNamecode;
	nameStr=selectName;
	
	[rootVc addEnterpriseVC:1000 enterType:enterdetailCode];
}
- (void)dealloc {
	[rootVc release];
	[enterName release];
	[enterdetailCode release];
	[listData release];
    [super dealloc];
}


@end
